/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CargaMasiva.java
 *
 * Created on Nov 16, 2011, 8:53:31 PM
 */
package GUI.CargaMasiva;


import BL.BLCargaMasiva;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.ParseException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 *
 * @author hunter2409
 */
public class CargaMasiva extends javax.swing.JDialog {
    BLCargaMasiva objGestorCargaMasiva;
    File objfile;

    /** Creates new form CargaMasiva */
    public CargaMasiva(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        objfile=null;
        objGestorCargaMasiva=null;
                
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlDatosArchivo = new javax.swing.JPanel();
        btnElegirArchivo = new javax.swing.JButton();
        txtRutaArchivo = new javax.swing.JTextField();
        btnCargar = new javax.swing.JButton();
        pnlDatosCargados = new javax.swing.JPanel();
        lbZonas = new javax.swing.JLabel();
        lbAlmacenes = new javax.swing.JLabel();
        lbPaletas = new javax.swing.JLabel();
        lbRacks = new javax.swing.JLabel();
        lbContactos = new javax.swing.JLabel();
        lbProductos = new javax.swing.JLabel();
        lbPallets = new javax.swing.JLabel();
        txtAlmacenes = new javax.swing.JTextField();
        txtZonas = new javax.swing.JTextField();
        txtRacks = new javax.swing.JTextField();
        txtPaletas = new javax.swing.JTextField();
        txtContactos = new javax.swing.JTextField();
        txtProductos = new javax.swing.JTextField();
        txtPallets = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        txtCargar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Carga Masiva");

        pnlDatosArchivo.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos Archivo"));

        btnElegirArchivo.setText("...");
        btnElegirArchivo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnElegirArchivoActionPerformed(evt);
            }
        });

        txtRutaArchivo.setEnabled(false);

        btnCargar.setText("Cargar...");
        btnCargar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCargarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlDatosArchivoLayout = new javax.swing.GroupLayout(pnlDatosArchivo);
        pnlDatosArchivo.setLayout(pnlDatosArchivoLayout);
        pnlDatosArchivoLayout.setHorizontalGroup(
            pnlDatosArchivoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlDatosArchivoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlDatosArchivoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlDatosArchivoLayout.createSequentialGroup()
                        .addComponent(txtRutaArchivo, javax.swing.GroupLayout.DEFAULT_SIZE, 267, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(btnElegirArchivo, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addComponent(btnCargar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );
        pnlDatosArchivoLayout.setVerticalGroup(
            pnlDatosArchivoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlDatosArchivoLayout.createSequentialGroup()
                .addGroup(pnlDatosArchivoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtRutaArchivo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnElegirArchivo, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(btnCargar, javax.swing.GroupLayout.PREFERRED_SIZE, 25, Short.MAX_VALUE)
                .addContainerGap())
        );

        pnlDatosCargados.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos Cargados"));

        lbZonas.setText("Zonas:");

        lbAlmacenes.setText("Almacen:");

        lbPaletas.setText("Paletas:");

        lbRacks.setText("Racks:");

        lbContactos.setText("Contactos:");

        lbProductos.setText("Productos:");

        lbPallets.setText("Pallets:");

        txtAlmacenes.setEditable(false);

        txtZonas.setEditable(false);

        txtRacks.setEditable(false);

        txtPaletas.setEditable(false);

        txtContactos.setEditable(false);

        txtProductos.setEditable(false);

        txtPallets.setEditable(false);

        javax.swing.GroupLayout pnlDatosCargadosLayout = new javax.swing.GroupLayout(pnlDatosCargados);
        pnlDatosCargados.setLayout(pnlDatosCargadosLayout);
        pnlDatosCargadosLayout.setHorizontalGroup(
            pnlDatosCargadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlDatosCargadosLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlDatosCargadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lbAlmacenes)
                    .addComponent(lbZonas)
                    .addComponent(lbRacks)
                    .addComponent(lbPaletas))
                .addGap(53, 53, 53)
                .addGroup(pnlDatosCargadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtPaletas)
                    .addComponent(txtZonas, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(txtRacks, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(txtAlmacenes, javax.swing.GroupLayout.DEFAULT_SIZE, 35, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(pnlDatosCargadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lbContactos)
                    .addComponent(lbProductos)
                    .addComponent(lbPallets))
                .addGap(41, 41, 41)
                .addGroup(pnlDatosCargadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtPallets)
                    .addComponent(txtProductos)
                    .addComponent(txtContactos, javax.swing.GroupLayout.DEFAULT_SIZE, 34, Short.MAX_VALUE))
                .addContainerGap(27, Short.MAX_VALUE))
        );
        pnlDatosCargadosLayout.setVerticalGroup(
            pnlDatosCargadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlDatosCargadosLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(pnlDatosCargadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbAlmacenes)
                    .addComponent(lbContactos)
                    .addComponent(txtAlmacenes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtContactos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(pnlDatosCargadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbZonas)
                    .addComponent(lbProductos)
                    .addComponent(txtZonas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtProductos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(pnlDatosCargadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbRacks)
                    .addComponent(lbPallets)
                    .addComponent(txtRacks, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtPallets, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(pnlDatosCargadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbPaletas)
                    .addComponent(txtPaletas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        jButton1.setText("Cancelar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        txtCargar.setText("Guardar");
        txtCargar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtCargarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(143, Short.MAX_VALUE)
                .addComponent(txtCargar, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtCargar, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(pnlDatosCargados, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(pnlDatosArchivo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnlDatosArchivo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlDatosCargados, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void btnElegirArchivoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnElegirArchivoActionPerformed
    
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setAcceptAllFileFilterUsed(false);
    FileFilter filter = new FileNameExtensionFilter("Archivos de carga masiva (*.txt)","txt");
    
    
    fileChooser.addChoosableFileFilter(filter);
    
    
    
    int ret = fileChooser.showDialog(null, "Seleccionar Archivo...");
    
    
    if (ret == JFileChooser.APPROVE_OPTION) {
      objfile = fileChooser.getSelectedFile();
      txtRutaArchivo.setText(objfile.toString());
    }
    
    
}//GEN-LAST:event_btnElegirArchivoActionPerformed

private void btnCargarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCargarActionPerformed
        objGestorCargaMasiva = new BLCargaMasiva(objfile);
        try {
            objGestorCargaMasiva.cargarDatos();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(CargaMasiva.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(CargaMasiva.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParseException ex) {
            Logger.getLogger(CargaMasiva.class.getName()).log(Level.SEVERE, null, ex);
        }
                
                txtAlmacenes.setText(objGestorCargaMasiva.getLstAmacenes().size()+"");
                txtContactos.setText(objGestorCargaMasiva.getLstContactos().size()+"");
                txtPaletas.setText(objGestorCargaMasiva.getLstPaletas().size()+"");
                txtPallets.setText(objGestorCargaMasiva.getLstPallets().size()+"");
                txtProductos.setText(objGestorCargaMasiva.getLstProductos().size()+"");
                txtRacks.setText(objGestorCargaMasiva.getLstRacks().size()+"");
                txtZonas.setText(objGestorCargaMasiva.getLstZonas().size()+"");
                
                
                
           
    
}//GEN-LAST:event_btnCargarActionPerformed

private void txtCargarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtCargarActionPerformed

    if (objGestorCargaMasiva!=null)
    {
        
        int result=JOptionPane.showConfirmDialog(null,"Desea cargar los datos al sistema?", "Registro Masivo",    JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
        
        if (result==JOptionPane.OK_OPTION)
        {
            objGestorCargaMasiva.llenarEnBaseDeDatos();
            objGestorCargaMasiva.internarPallets();
            JOptionPane.showMessageDialog(null,"Se cargaron los datos.","Carga Masiva",JOptionPane.INFORMATION_MESSAGE);
        
        }
    }
    else
    {
         JOptionPane.showMessageDialog(null,"Se cargaron los datos..","Carga Masiva",JOptionPane.INFORMATION_MESSAGE);
    }
    
    
}//GEN-LAST:event_txtCargarActionPerformed

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    this.dispose();
}//GEN-LAST:event_jButton1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(CargaMasiva.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(CargaMasiva.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(CargaMasiva.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(CargaMasiva.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                CargaMasiva dialog = new CargaMasiva(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCargar;
    private javax.swing.JButton btnElegirArchivo;
    private javax.swing.JButton jButton1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JLabel lbAlmacenes;
    private javax.swing.JLabel lbContactos;
    private javax.swing.JLabel lbPaletas;
    private javax.swing.JLabel lbPallets;
    private javax.swing.JLabel lbProductos;
    private javax.swing.JLabel lbRacks;
    private javax.swing.JLabel lbZonas;
    private javax.swing.JPanel pnlDatosArchivo;
    private javax.swing.JPanel pnlDatosCargados;
    private javax.swing.JTextField txtAlmacenes;
    private javax.swing.JButton txtCargar;
    private javax.swing.JTextField txtContactos;
    private javax.swing.JTextField txtPaletas;
    private javax.swing.JTextField txtPallets;
    private javax.swing.JTextField txtProductos;
    private javax.swing.JTextField txtRacks;
    private javax.swing.JTextField txtRutaArchivo;
    private javax.swing.JTextField txtZonas;
    // End of variables declaration//GEN-END:variables
}
